package org.jsola.contract.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.contract.constant.EnterpriseSealType;
import org.jsola.contract.constant.FontType;
import org.jsola.contract.constant.PersonSealType;
import org.jsola.contract.constant.SealColor;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.contract.example.SealExample.E_BOTTOM_TEXT;
import static org.jsola.contract.example.SealExample.E_COLOR;
import static org.jsola.contract.example.SealExample.E_CONTENT;
import static org.jsola.contract.example.SealExample.E_DESCRIPTION;
import static org.jsola.contract.example.SealExample.E_ENTERPRISE_SEAL_TYPE;
import static org.jsola.contract.example.SealExample.E_FONT;
import static org.jsola.contract.example.SealExample.E_FONT_SIZE;
import static org.jsola.contract.example.SealExample.E_NAME;
import static org.jsola.contract.example.SealExample.E_PERSONAL_SEAL_TYPE;
import static org.jsola.contract.example.SealExample.E_ROUND_TEXT;
import static org.jsola.contract.example.SealExample.E_ROW_TEXT;
import static org.jsola.contract.example.SealExample.E_SEAL_STRING;
import static org.jsola.contract.example.SealExample.E_SOURCE_ID;
import static org.jsola.contract.example.SealExample.E_STATUS;
import static org.jsola.contract.example.SealExample.E_SUBJECT_ID;
import static org.jsola.contract.example.SealExample.E_TEXT;
import static org.jsola.contract.example.SealExample.N_BOTTOM_TEXT;
import static org.jsola.contract.example.SealExample.N_COLOR;
import static org.jsola.contract.example.SealExample.N_CONTENT;
import static org.jsola.contract.example.SealExample.N_DESCRIPTION;
import static org.jsola.contract.example.SealExample.N_ENTERPRISE_SEAL_TYPE;
import static org.jsola.contract.example.SealExample.N_FONT;
import static org.jsola.contract.example.SealExample.N_FONT_SIZE;
import static org.jsola.contract.example.SealExample.N_NAME;
import static org.jsola.contract.example.SealExample.N_PERSONAL_SEAL_TYPE;
import static org.jsola.contract.example.SealExample.N_ROUND_TEXT;
import static org.jsola.contract.example.SealExample.N_ROW_TEXT;
import static org.jsola.contract.example.SealExample.N_SEAL_STRING;
import static org.jsola.contract.example.SealExample.N_SOURCE_ID;
import static org.jsola.contract.example.SealExample.N_STATUS;
import static org.jsola.contract.example.SealExample.N_SUBJECT_ID;
import static org.jsola.contract.example.SealExample.N_TEXT;

/**
 * 印章
 *
 * @author JanusMix
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SealQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_SUBJECT_ID, example = E_SUBJECT_ID)
    @Length(max = 32)
    private String subjectId;

    @ApiParam(value = N_NAME, example = E_NAME)
    @Length(max = 255)
    private String name;

    @ApiParam(value = N_SOURCE_ID, example = E_SOURCE_ID)
    @Length(max = 32)
    private String sourceId;

    @ApiParam(value = N_DESCRIPTION, example = E_DESCRIPTION)
    @Length(max = 1000)
    private String description;

    @ApiParam(value = N_SEAL_STRING, example = E_SEAL_STRING)
    @Length(max = 2147483647)
    private String sealString;

    @ApiParam(value = N_CONTENT, example = E_CONTENT)
    @Length(max = 255)
    private String content;

    @ApiParam(value = N_COLOR, example = E_COLOR, allowableValues = SealColor.VALUES_STR)
    @Length(max = 30)
    private String color;

    @ApiParam(value = N_FONT, example = E_FONT, allowableValues = FontType.VALUES_STR)
    @Length(max = 50)
    private String font;

    @ApiParam(value = N_FONT_SIZE, example = E_FONT_SIZE)
    @Length(max = 30)
    private String fontSize;

    @ApiParam(value = N_PERSONAL_SEAL_TYPE, example = E_PERSONAL_SEAL_TYPE, allowableValues = PersonSealType.VALUES_STR)
    @Length(max = 100)
    private String personalSealType;

    @ApiParam(value = N_ENTERPRISE_SEAL_TYPE, example = E_ENTERPRISE_SEAL_TYPE, allowableValues = EnterpriseSealType.VALUES_STR)
    @Length(max = 100)
    private String enterpriseSealType;

    @ApiParam(value = N_TEXT, example = E_TEXT)
    @Length(max = 255)
    private String text;

    @ApiParam(value = N_ROUND_TEXT, example = E_ROUND_TEXT)
    @Length(max = 255)
    private String roundText;

    @ApiParam(value = N_ROW_TEXT, example = E_ROW_TEXT)
    @Length(max = 255)
    private String rowText;

    @ApiParam(value = N_BOTTOM_TEXT, example = E_BOTTOM_TEXT)
    @Length(max = 255)
    private String bottomText;

    @ApiParam(value = N_STATUS, example = E_STATUS)
    private Integer status;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public SealQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

